package com.example.guigu.getway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author 叶家康
 * @version 1.0
 * @since jdk 1.8
 */
@Configuration
public class HeiMaCorsConfiguation {
     @Bean
    public CorsWebFilter corsWebFilter(){
         UrlBasedCorsConfigurationSource source =new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration =new CorsConfiguration();


        //配置跨域  先发送一个options请求，服务器返回允许的时候，
         // 浏览器才发送post、get等真实的请求，最后服务器返回数据


         //允许那些头跨域
        corsConfiguration.addAllowedHeader("*");
        //允许那些请求方式
        corsConfiguration.addAllowedMethod("*");
        //允许那些请求来源
        corsConfiguration.addAllowedOrigin("*");
        //是否携带cookie跨域
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**",corsConfiguration);


        return new CorsWebFilter(source);
    }
}
